{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 通过tf.train.import_meta_graph获取模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Restoring parameters from Saved_model/model.ckpt\n",
      "v1:0 : [ 1.]\n",
      "v2:0 : [ 2.]\n",
      "v1 VariableV2 :\n",
      "[ 1.]\n",
      "##########\n",
      "v2 VariableV2 :\n",
      "[ 2.]\n",
      "##########\n",
      "add Add :\n",
      "[ 3.]\n",
      "##########\n",
      "v1: [ 1.]\n",
      "v2: [ 2.]\n",
      "add: [ 3.]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "saver = tf.train.import_meta_graph(\"Saved_model/model.ckpt.meta\")\n",
    "with tf.Session() as sess:\n",
    "    saver.restore(sess, \"Saved_model/model.ckpt\")\n",
    "    for v in tf.global_variables():\n",
    "        print(v.name, \":\", v.eval())\n",
    "    for op in tf.get_default_graph().get_operations():\n",
    "        if op.type == \"VariableV2\" or op.type == \"Add\":\n",
    "            print(op.name, op.type, \":\")\n",
    "            for o in op.outputs:\n",
    "                print(o.eval())\n",
    "            print(\"##########\")    \n",
    "    print(\"v1:\", sess.run(tf.get_default_graph().get_tensor_by_name(\"v1:0\")))\n",
    "    print(\"v2:\", sess.run(tf.get_default_graph().get_tensor_by_name(\"v2:0\")))\n",
    "    print(\"add:\", sess.run(tf.get_default_graph().get_tensor_by_name(\"add:0\")))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 通过NewCheckpointReader获取变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "varname: v2 ,varshape: [1] ,value: [ 2.]\n",
      "varname: v1 ,varshape: [1] ,value: [ 1.]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "reader = tf.train.NewCheckpointReader(\"Saved_model/model.ckpt\")\n",
    "\n",
    "# 获取所有变量列表\n",
    "all_vars = reader.get_variable_to_shape_map()\n",
    "for var_name in all_vars:\n",
    "    print(\"varname:\", var_name, \",varshape:\", all_vars[var_name], \",value:\", reader.get_tensor(var_name))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
